package com.domdd.controller.open;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.DateParser;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.domdd.controller.base.resp.BasePagingResp;
import com.domdd.controller.base.resp.BaseResp;
import com.domdd.controller.req.OpenInventoryNewListReq;
import com.domdd.model.InventoryNew;
import com.domdd.service.PageService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @Author David.Liu
 * @Date 2024-12-06 13:32
 **/
@RestController
@Slf4j
@Api(tags = "[page]")
@RequestMapping("page")
@RequiredArgsConstructor
public class PageController {
    private final PageService pageService;

    @PostMapping("inventoryNewList")
    @ApiOperation("获取新库存")
    @ApiOperationSupport(author = "lw")
    public BaseResp<BasePagingResp<InventoryNew>> inventoryNewList(@RequestBody OpenInventoryNewListReq req) {
        log.info("[inventoryNewList/list] params:{}", JSON.toJSONString(req));
        BasePagingResp<InventoryNew> resp = new BasePagingResp<>(pageService.inventoryNewList(DateUtil.beginOfDay(req.getInventoryDate()), req.getPage(), req.getPageSize()));
        return BaseResp.success(resp);
    }

    @RequestMapping("exportInventoryNew")
    @ApiOperation("导出新库存")
    @ApiOperationSupport(author = "lw")
    public void exportExistCategoryLang(@RequestParam("inventoryDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date inventoryDate, HttpServletResponse response) throws IOException {
        if (ObjectUtil.isNull(inventoryDate)) {
            throw new RuntimeException("时间必选");
        }
        pageService.exportExistCategoryLang(DateUtil.offsetDay(inventoryDate, 1), response);
    }
}
